package netty.rpc.framework.core.netty.consumer;

import netty.rpc.framework.core.netty.transport.protocol.RPCResponse;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * rpc请求的结果存放处
 * @author guiyiqiao
 * @date 2020/12/7
 */
public class UncompletedRequest {
    private  Map<Integer, CompletableFuture<RPCResponse>> completableFutureMap = new ConcurrentHashMap<>();

    public  void addFuture(int requestId,CompletableFuture<RPCResponse> future){
        completableFutureMap.put(requestId,future);
    }

    public  void completeFuture(int requestId,RPCResponse rpcResponse){
        CompletableFuture<RPCResponse> rpcResponseCompletableFuture = completableFutureMap.get(requestId);
        if(rpcResponseCompletableFuture == null){
            return ;
        }
        rpcResponseCompletableFuture.complete(rpcResponse);
        completableFutureMap.remove(requestId);
    }
}
